package org.pinda.gui;

import static org.johnnei.engine.render.RenderObject.VERTEX_COLOR;

import org.johnnei.engine.Game;
import org.johnnei.engine.render.ColorHelper;
import org.johnnei.engine.render.TextRenderer;
import org.johnnei.engine.render.VertexHelper;
import org.pinda.gui.loading.ILoadingThread;

public class GuiLoading extends Gui {
	
	/**
	 * The current loading thread
	 */
	private ILoadingThread loadThread;
	/**
	 * The current phase of the loading thread
	 */
	private int progressPhase;
	
	public GuiLoading(Gui parent, ILoadingThread loadThread) {
		super(parent, VERTEX_COLOR);
		this.loadThread = loadThread;
		generateVertexData();
		generateColorData();
	}
	
	@Override
	public void generateVertexData() {
		final float width = 3 * loadThread.getProgress();
		final float height = 20F;
		final float x = (Game.WIDTH / 2) - (width / 2);
		final float y = (Game.HEIGHT / 2) - (height / 2);
		renderObject.updateVertex(new VertexHelper(x, y, width, height));
	}
	
	@Override
	public void generateColorData() {
		renderObject.updateColor(new ColorHelper(0, 255, 0));
	}
	
	@Override
	public void onTick(int deltaMs) {
		super.onTick(deltaMs);
		loadThread.onTick(progressPhase++);
		if(loadThread.getProgress() == 100) {
			switchGui(loadThread.getGui());
		} else {
			generateVertexData();
		}
	}
	
	@Override
	public void render() {
		final float x = Game.WIDTH / 2;
		final float y = (Game.HEIGHT / 2) - 40;
		TextRenderer.drawCentered(x, y, loadThread.getStatus());
		super.render();
	}

}
